//
//  Person.m
//  OC02_类和对象
//
//  Created by Hanlingyu on 16/7/5.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import "Person.h"

@implementation Person


- (void)sleep:(NSInteger)time {
    // NSLog 属于C语言的函数
    NSLog(@"这个人睡了%ld年", time);

}

- (void)setName:(NSString *)name {
    if (_name != name) {
        [_name release];
        _name = [name retain];
    }
    //_name = name; ARC下这样写
}

- (NSString *)name {
    
    return [[_name retain] autorelease];
}

- (void)setAge:(NSInteger)age {
    _age = age;
}

- (NSInteger)age {
    return _age;
}


// 命名规范:
// 实现大括号前要加空格/回车

// 指派初始化方法
// 无论调用哪个初始化方法, 都会执行的初始化方法, 称为指派初始化方法
// 对于后面连个初始化方法, 此初始化方法就是指派初始化方法, 即无论调用哪个初始化方法, 都会调用此方法
- (instancetype)initWithName:(NSString *)name
                         age:(NSInteger)age {
    self = [super init];
    if (self) {
        _name = name;
        _age = age;
    }
    return self;
}

- (instancetype)initWithName:(NSString *)name {
    return [self initWithName:name age:0];
}

- (instancetype)initWithAge:(NSInteger)age {
    return [self initWithName:nil age:age];
}

+ (instancetype)personWithName:(NSString *)name age:(NSInteger)age {
    return [[[Person alloc] initWithName:name age:age] autorelease];
}



@end
